<template>
  <div class="body">
    <el-form ref="ruleForm" :model="mdl" label-width="130px">

      <el-form-item label="类型图片" required>
        <el-upload
          ref="upload"
          list-type="picture-card"
          action
          :file-list="fileList"
          :show-file-list="true"
          :http-request="uploadFile"
          accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"
          :limit="1"
          :on-change="handlePreview"
          :on-exceed="handleExceed"
          :on-remove="handeRemove"
        >
          <i class="el-icon-plus" />
        </el-upload>
      </el-form-item>
      <el-form-item label="类型名称" required>
        <el-input
          v-model="mdl.name"
          placeholder="请输入类型名称"
        />
      </el-form-item>
      <el-form-item label="店铺" required>
        <el-select v-model="mdl.storeId" placeholder="请选择">
          <el-option
            v-for="item in storeList"
            :key="item.storeId"
            :label="item.storeName"
            :value="item.storeId"
          />
        </el-select>
      </el-form-item>

    </el-form>
    <div class="foot_btn">
      <el-button
        type="primary"
        :loading="submitLoading"
        @click="submitForm('ruleForm')"
      >提交</el-button>
      <el-button @click="handleCloseDrawer">取消</el-button>
    </div>
  </div>
</template>

<script>
import { fileUpload, pageStore, addGoodsType, updateGoodsType } from '@/api/user'
export default {
  name: 'FormModel',
  props: {
    mdl: {
      type: Object,
      required: true
    },
    handleCloseDrawer: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      submitLoading: false,
      fileList: [],
      uploadData: null,
      img: '',
      storeList: []
    }
  },
  mounted() {
    if (this.mdl.icon) {
      this.img = this.mdl.icon
      this.fileList.push({
        url: this.mdl.icon
      })
    }

    this.getPageStore()
  },
  methods: {
    // 店铺列表
    getPageStore() {
      pageStore({ current: 1, size: 20 }).then(res => {
        if (res.statusCode == '00000') {
          this.storeList = res.data.records
        }
      })
    },

    // 移除上传图片
    handeRemove(e) {
      this.imgList = ''
    },

    // 上传图片
    uploadFile(e) {
      fileUpload(this.uploadData).then((res) => {
        if (res) {
          // this.mdl.roomImg = res.data
          this.img = res.data
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          console.log(res)
        } else {
          this.$message.error(res.status)
        }
      })
    },
    handleExceed(file) {
      this.$message({
        message: '类型图片只能上传一张',
        type: 'warning'
      })
    },
    // 获取上传文件信息
    handlePreview(file) {
      const _this = this
      var formData = new FormData()
      formData.append('file', file.raw)
      _this.uploadData = formData
    },

    // 提交
    submitForm() {
      this.mdl.icon = this.img
      console.log(this.mdl)
      const request = this.mdl.id ? updateGoodsType(this.mdl) : addGoodsType(this.mdl)
      request.then((res) => {
        if (res.statusCode === '00000') {
          this.$message({ message: '提交成功', type: 'success' })
          // 关闭弹窗
          this.handleCloseDrawer()
          // 调用父级组件刷新数据
          this.$emit('getList')
        }
      })
        .catch((error) => {
          console.log(error)
        })
        .finally(() => {
        })
    }
  }
}
</script>

<style lang="less" scoped>
.body {
  padding-right: 25px;
  .foot_btn{
    width:150px;
    margin: 0 auto;
  }
}

/deep/ .el-checkbox__label {
  font-size: 12px;
}
</style>
